﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelliCalculator
{
    class Program
    {
        static int pointer = 0;
        static double[] dArray = new double[64];
        static double[] weight;
        static int count = dArray.Length;
        static ConsoleReader creader = new ConsoleReader();
        static string mainInformation = @"
------------IntelliCalculator------------
                  ver.  1.0 Pre-Alpha v3
///////   输入help来查看所有帮助  \\\\\\\";
        static string helpInformation = @"
----------------  Help   ----------------
    命令                        释义
   WAVERAGE                求加权平均数
   SORT                    数据排序
   AVERAGE                 求平均数
   MODE                    求众数
   MEDIAN                  求中位数
   RANGE                   求极差
   VARIANCE                求方差
   STDD                    求标准差 
   INPUT                   数据录入


   注意：
        求加权平均数时，权可以是浮点数，
        但需要额外录入，退出后则清空保存
        数据。";
        static void Main(string[] args)
        {
            Console.Title = "IntelliCalculator .NET Pre-Alpha v2";
            Console.WriteLine(mainInformation);
            while (true)
            {
                switch (creader.ReadCommand().ToLower())
                {
                    case "exit":
                        Environment.Exit(0);
                        break;
                    case "clear":
                        Console.Clear();
                        break;
                    case "help":
                        Help();
                        break;
                    case "average":
                        Average();
                        break;
                    case "waverage":
                        Waverage();
                        break;
                    case "mode":
                        Mode();
                        break;
                    case "input":
                        Input();
                        break;
                    case "median":
                        Median();
                        break;
                    case "show":
                        Show();
                        break;
                    case "sort":
                        Sort();
                        break;
                    case "range":
                        Range();
                        break;
                    case "variance":
                        Variance();
                        break;
                    case "stdd":
                        Stdd();
                        break;
                    default:
                        Console.WriteLine("找不到命令。");
                        break;
                }
            }
        }

        static void Help()
        {
            Console.Clear();
            Console.WriteLine(helpInformation);
            Console.WriteLine("请按任意键继续...");
            Console.ReadKey();
            Console.Clear();
        }

        protected static void Waverage()
        {
            string command;
            double[] temp = creader.ReadDoubleArray(out command,false);
        }

        protected static void Average()
        {
            Show();
            double value = 0;
            foreach (double d in ReadArray())
                value += d;
            Console.WriteLine("以上数据的平均数为{0}", value / pointer);
        }

        protected static double GetAverage()
        {
            double value = 0;
            foreach (double d in ReadArray())
                value += d;
            return value / pointer;
        }

        protected static void Mode()
        {

        }

        protected static void Range()
        {
            Sort();
            Show();
            double[] tempDArray = ReadArray();
            Console.WriteLine("以上数据的极差为{0}", tempDArray[tempDArray.Length - 1] - tempDArray[0]);
        }

        protected static void Variance()
        {
            Show();
            double average = GetAverage();
            double temp = 0;
            double[] array = ReadArray();

            foreach (double d in array)
            {
                temp += (d - average) * (d - average);
            }

            double result = temp / array.Length;
            Console.WriteLine("以上数据的方差为{0}", result);
        }

        protected static double GetVariance()
        {
            double average = GetAverage();
            double temp = 0;
            double[] array = ReadArray();

            foreach (double d in array)
            {
                temp += (d - average) * (d - average);
            }

            double result = temp / array.Length;
            return result;
        }

        protected static void Stdd()
        {
            Show();
            Console.WriteLine("以上数据的标准差为{0}", Math.Sqrt(GetVariance()));
        }

        protected static void Sort()
        {
            double[] sortedArray = ReadArray();
            Array.Sort(sortedArray);
            dArray = sortedArray;
        }

        protected static void Median()
        {
            Sort();
            Show();
            if (IsOdd())
            {
                Console.WriteLine("以上数据的中位数为{0}", ReadArray()[(pointer + 1) / 2]);
            }
            else
            {
                double x1 = ReadArray()[pointer/2-1];
                double x2 = ReadArray()[pointer/2];
                Console.WriteLine("以上数据的中位数为{0}", (x1 + x2) / 2);
            }
        }

        private static bool IsOdd()
        {
            if (pointer % 2 == 0)
                return false;
            else
                return true;
        }

        protected static void Input()
        {
            while (true)
            {
                string temp = "";
                try
                {
                    dArray[pointer] = creader.ReadDoubleArray(out temp, false)[0];
                    pointer++;
                }
                catch (NoAvailableValueException)
                {
                    Console.WriteLine("输入错误");
                }
                catch (ExitMessage)
                {
                    return;
                }
                catch (ClearMessage)
                {
                    Console.Clear();
                }
            }
        }

        protected static void Show()
        {
            foreach (double d in ReadArray())
                Console.WriteLine(d);
            return;
        }

        private static double[] ReadArray()
        {
            double[] dArrayTemp = new double[pointer];

            for (int i = 0; i <= pointer - 1; i++)
            {
                dArrayTemp[i] = dArray[i];
            }

            return dArrayTemp;
        }

        private static void WriteArray()
        {

        }
    }
}
